package top.scsoul.boot.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import top.scsoul.boot.service.SysUserService;
import top.scsoul.boot.utils.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class BaseController {
    Logger logger = Logger.getLogger(BaseController.class);
    @Autowired
    SysUserService sysUserService;


    @GetMapping("/")
    public String index() {
        return "redirect:login";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String loginCheck(String usercode, String password, Model model) {
        StringUtil.isNullOrEmptyThrowException(usercode, password);
        logger.info(usercode);
        logger.info(password);
        try {
            sysUserService.loginCheck(usercode, password);
            return "redirect:customer";
        } catch (Exception e) {
            logger.error(e.getMessage());
            model.addAttribute("err",e.getMessage());
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "redirect:login";
    }


}
